package com.xls.pet.controller;

import com.mybatisflex.core.paginate.Page;
import com.xls.pet.common.Result;
import com.xls.pet.entity.ItemStock;
import com.xls.pet.query.ItemStockQuery;
import com.xls.pet.service.ItemStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 控制层。
 *
 * @author xls
 * @since 2024-06-17
 */
@RestController
@RequestMapping("/itemStock")
public class ItemStockController {

    @Autowired
    private ItemStockService itemStockService;

    @GetMapping("/{id}")
    public Result<?> getItemStock(@PathVariable Integer id) {
        return Result.success(itemStockService.getById(id));
    }

    /**
     * 添加。
     *
     * @param itemStock
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public Result<?> save(@RequestBody ItemStock itemStock) {
        itemStockService.saveItemStock(itemStock);
        return Result.success();
    }

    /**
     * 根据主键删除。
     *
     * @param ids 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{ids}")
    public Result<?> remove(@PathVariable Integer[] ids) {
        itemStockService.removeByIds(List.of(ids));
        return Result.success();
    }

    /**
     * 根据主键更新。
     *
     * @param itemStock
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public Result<?> update(@RequestBody ItemStock itemStock) {
        itemStockService.updateItemStock(itemStock);
        return Result.success();
    }

    @PostMapping("/list/{pageNumber}/{pageSize}/{totalRow}")
    public Result<Page<ItemStock>> list(@PathVariable Integer pageNumber, @PathVariable Integer pageSize,
                                        @PathVariable Integer totalRow, @RequestBody ItemStockQuery itemStockQuery) {
        return Result.success(itemStockService.list(pageNumber, pageSize, totalRow, itemStockQuery));
    }

}
